/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var isEdit = false;
var parent;
var singleselection = "SINGLE_SELECTION";
var multichoice = "MULTI_CHOICE";
var singleline = "SINGLE_LINE";
var multiline = "MULTI_LINE";
var radioName = 1;
zm.ready(function() {





/// end home sort///

//////////////////////




///////////////////////////pading//////////////////////////////




    ///////////////////////////pading//////////////////////////////


    function showPage(pageContainer, pageNum)
    {
        pageContainer.attr("curr", pageNum);
        managePageNumbers(pageContainer, pageNum);
    }

    zm('.Paging').each(function() {
        var pageContainer = zm(this);
        managePageNumbers(pageContainer, 1);
        zm(".moveFirst").bind("click", function() {

            var pageContainer = zm(this).parent();
            pageContainer.attr("curr", 1);
            startAjax(pageContainer, 1);
            showPage(pageContainer, 1);
        });
        zm(".moveLast").bind("click", function() {

            var pageContainer = zm(this).parent();
            var pgCount = pageContainer.attr("count");
            pageContainer.attr("curr", pgCount);
            startAjax(pageContainer, pgCount);
            showPage(pageContainer, pgCount);
        });

//        var page = pageContainer.find(".pageNumbers");
//        //page.unbindAll();
//        page.find("a").each(function() {    
//            zm(this).bind("click", function() {
//                alert(zm(this).text());
//                var pageContainer = zm(this).parent().parent();
//                showPage(pageContainer,zm(this).text())
//            });
//        });
    });



    function managePageNumbers(pageContainer, pageNum)
    {
        var currPage = pageContainer.attr("curr");
        var pgCount = pageContainer.attr("count");
        var cnt = 0;
        var temp = "";
        for (var i = currPage - 5; i <= currPage + 10; i++)
        {
            if (cnt === 10 || i > pgCount)
                break;
            if (i >= 1 && i <= pgCount) {
                if (i === parseInt(pageNum)) {
                    temp += " <a href='javascript:void(0);' class='active'>" + i + "</a> ";
                }
                else {
                    temp += " <a href='javascript:void(0);'>" + i + "</a> ";
                }
                cnt++;
            }
        }


        var page = pageContainer.find(".pageNumbers");
        page.unbindAll();
        page.html(temp);
        page.find("a").each(function() {
            zm(this).bind("click", function() {
                zm(this).addClass("active");
                startAjax(pageContainer, zm(this).text());
                showPage(pageContainer, zm(this).text());
            });
        });
    }
//////////////////////////////////////////////////////////////



////////////////paging ajaxx////////////////////
    function startAjax(pageContainer, page) {
        if (pageContainer.attr("name") === "paging_new") {
            ajaxNewestHome(pageContainer, page);
        }
        if (pageContainer.attr("name") === "paging_hot") {
            ajaxHostestHome(pageContainer, page);
        }
        if (pageContainer.attr("name") === "paging_stat") {
            ajaxStat(pageContainer, page);
        }
        if (pageContainer.attr("name") === "paging_res") {
            ajaxResDetail(pageContainer, page);
        }

    }


/////////////////////////////////////////////////

/////////////////////////datepicker////////////////////////

    zm("#date-start").focus(function() {
        zm(".calendar").remove();
        new datepickr('date-start', {
            'dateFormat': 'd/m/Y'
        });

    });
    zm("#date-end").focus(function() {
        zm(".calendar").remove();
        new datepickr('date-end', {
            'dateFormat': 'd/m/Y'
        });

    });
    zm(".page").click(function() {
        var tableparent = zm(this).parent().parent().find('.chartContainer');
        var page = zm(this).text();
        var idSurvey = zm('#survey').attr('idsurvey');
        var idQuestion = tableparent.parent().attr('idquestion');

        zm.ajax({
            method: "POST",
            url: "answer?idSurvey=" + idSurvey + "&idQuestion=" + idQuestion + "&page=" + page,
            success: function(data) {
                tableparent.html(data);
            }
        });
    });
});

function balance(left, right) {
    var leftPosition = left.position();
    var leftWith = left.width();
    var leftHeight = left.height();
    if (leftHeight > right.height()) {
        var hi = right.height();
        var test = leftPosition.top + (leftHeight - right.height() / 2);

        right.css("margin-top", ((leftHeight - right.height()) / 2) + "px");

    }

}

